#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
from datetime import datetime
from typing import Optional,List


from sqlalchemy import func, String,Float,Integer,ForeignKey,DateTime,ARRAY,TEXT,TypeDecorator
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.orm import Mapped, mapped_column,relationship

from app.database.base_class import  id_key, DataClassBase

class JSONEncodedList(TypeDecorator):
    impl = String(200)

    def process_bind_param(self, value , dialect ):
        if value is not None:
            value = json.dumps(value)
        else:
            value = []
        return value

    def process_result_value(self, value, dialect ) :
        if value is not None:
                value = json.loads(value)
        else:
            value = json.loads("[]")
        return value




class User(DataClassBase):
    """ 用户表 """
    """

        "id": 23998,
        "boss_level": "99",
        "boss_logonid": "yungchiangwang",
        "boss_no": "1109008",
        "class_id": "常日班",
        "class_no": "1.0",

        "e_mail": "xiaosong.zhuo@auo.com",
        "emp_name": "卓松",
        "emp_no": "K2207368",
        "emp_type": "一般员工",
        "eng_name": "xiaosong zhuo 卓松",
        "job_idl": "IDL",
        "loginid": "xiaosongzhuo",
        "org_id": "ML6KA3",
        "sex": "M",
        "title": "工程师",
        "come_date": "7/4/2022 12:00:00 AM",
        "active": "Y",
        "quit_date": "None"
    """
    __tablename__ = 'user_table'



    id: Mapped[id_key] = mapped_column(init=False)
    # id: Mapped[int] = mapped_column(Integer,comment='id编号')
    boss_level:Mapped[int]= mapped_column(Integer,comment='老板等级,数字越小,等级越高,工程师是99,副理30,经理20,厂长是6,最高级别是0')
    boss_logonid:Mapped[str]= mapped_column(String(20),  comment='老板的loginid')
    boss_no:Mapped[str]= mapped_column(String(20),  comment='老板的工号')
    class_id:Mapped[str]= mapped_column(String(10),comment='常日班,长白班')
    e_mail:Mapped[str]= mapped_column(String(50),unique=True,comment='邮箱')
    emp_name:Mapped[str]= mapped_column(String(20),comment='中文姓名')
    emp_no:Mapped[str]= mapped_column(String(20),comment='工号')
    emp_type:Mapped[str]= mapped_column(String(20),comment='一般员工,派如员工')
    eng_name:Mapped[str]= mapped_column(String(50),comment='英文加中文名称')

    job_idl:Mapped[str]= mapped_column(String(20),comment='idl,dl')
    loginid:Mapped[str]= mapped_column(String(20),unique=True,comment='当前用户的loginid')
    org_id:Mapped[str]= mapped_column(String(20),comment='所在的部分,小部门')
    sex:Mapped[str]= mapped_column(String(5),comment='性别')
    title:Mapped[str]= mapped_column(String(5),comment='职称,工程师,副理,经理,总经理,董事长...')
    come_date:Mapped[str]= mapped_column(String(50),comment='进入公司的日期')
    roles:Mapped[JSONEncodedList] = mapped_column(JSONEncodedList,nullable=True,comment='角色')
    
    last_login: Mapped[Optional[datetime]] = mapped_column(init=False, onupdate=func.now(), comment='上次登录')








